﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DevScope.SharePoint.Utils.BulkDelete.Helpers.Caml;
using DevScope.SharePoint.Utils.BulkDelete.Helpers.Caml.Enumerations;
using System.Xml;
using System.Net;
using DevScope.SharePoint.Utils.BulkDelete.Helpers;
using System.Diagnostics;

namespace DevScope.SharePoint.Utils.BulkDelete.Testing
{
    /// <summary>
    /// Summary description for Test
    /// </summary>
    [TestClass]
    public class Tests
    {
        public Tests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        private const int ITEM_COUNT = 400;
        private const string WEB_URL = "http://localhost:555/AttachControl";
        private const string LIST_NAME = "TesteDelete";
        private const string BULK_DELETE_APP_PATH = "BulkDelete.exe";
        private const string BULK_DELETE_APP_ARGS1 = "-web http://localhost:555/AttachControl -list TesteDelete -filter TestFilter.config -retries 5 -batchSize 100";

        [TestMethod]
        public void AddItemsToList()
        {
            using (SPWSLists.Lists proxy = new SPWSLists.Lists())
            {
                //proxy.Credentials = new NetworkCredential("Rui Romano", "rrsr5891", "RomanoDevScope");
                proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
                proxy.Url = WebHelper.UrlCombine(WEB_URL, "_vti_bin/lists.asmx");
                //O proxy é reutilizado para verificar se o item existe
                XmlNode batchNode = BuildCamlUpdateBatch();

                XmlNode ndResult = proxy.UpdateListItems(LIST_NAME, batchNode);
            }
        }


        [TestMethod]
        public void RunBulkDeleteTool()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            
            startInfo.FileName = BULK_DELETE_APP_PATH;
            startInfo.Arguments = BULK_DELETE_APP_ARGS1;

            using (Process installProcess = Process.Start(startInfo))
            {                
                installProcess.WaitForExit();                
            }
            
        }

        private static XmlNode BuildCamlUpdateBatch()
        {
            CamlBuilder updateBatchBuilder = new CamlBuilder(CamlTypeEnum.UpdateListItems);

            //Em caso de erro para o processo de update...
            updateBatchBuilder.UpdateBatchNode("OnError", CamlOnErrorTypeEnum.Return.ToString());

            for (int i = 1; i <= ITEM_COUNT; i++)
            {
                updateBatchBuilder.AddMethodNode(i, CamlCommandTypeEnum.New);
                
                updateBatchBuilder.AddFieldNode(i, "Title", "Teste " + i);                

            }         

            return updateBatchBuilder.BatchNode;
        }
    }
}
